<?php
/**
 * Created by PhpStorm.
 * User: kaikaic
 * Date: 2018/10/28
 * Time: 21:33
 */

namespace app\api\model;

use app\psy\service\OrderService;
use model\BasicModel;

class OrderModel extends BasicModel {

    private $service;

    public function __construct() {
        parent::__construct();

        $this->service = OrderService::getInstance();
    }

    // 订单是否已存在
    public function isOrderExist($condition) {
        return $this->service->single($condition);
    }
    public function orderInfo($id) {
        return $this->service->find($id);
    }

    /**
     * 生成预约订单
     */
    public function createOrder($params) {
        $accountID = $params['account_id'];

        $payOrder = $this->createPayOrder($accountID);
        if (!$payOrder) {
            return 0;
        }
        $params['pay_sn'] = $payOrder['pay_sn'];
        $params['order_sn'] = self::makeOrderSn($payOrder['id']);

        return $this->service->insert($params);
    }
    /**
     * 生成团检预约订单
     */
    public function createTjOrder($params) {
        $accountID = $params['account_id'];

        $type = $params['type'];

        $payOrder = $this->createPayOrder($accountID,$type);
        if (!$payOrder) {
            return 0;
        }
        $params['pay_sn'] = $payOrder['pay_sn'];
        $params['order_sn'] = self::makeOrderSn($payOrder['id']);

        $res = $this->service->insert($params);
        if($res){
            return  $params['order_sn'] ;
        }else{
            return  $res ;

        }
    }
    /**
     * 生成支付订单
     */
    public function createPayOrder($accountID) {
        $payOrder = array();
        $payOrder['pay_sn'] = self::makePaySn($accountID);
        $payOrder['account_id'] = $accountID;
        $payOrder['api_pay_state'] = 0;
        $payOrder['create_time'] = time();


        $id = $this->service->insert($payOrder, 1);
        if ($id > 0) {
            $payOrder['id'] = $id;
            return $payOrder;
        }

        return null;
    }

    /**
     * 生成支付单编号(两位随机 + 从2000-01-01 00:00:00 到现在的秒数+微秒+会员ID%1000)，该值会传给第三方支付接口
     * 长度 =2位 + 10位 + 3位 + 3位  = 18位
     * 1000个会员同一微秒提订单，重复机率为1/100
     * @return string
     */
    public static function makePaySn($accountID) {
        return mt_rand(10, 99)
        . sprintf('%010d', time() - 946656000)
        . sprintf('%03d', (float)microtime() * 1000)
        . sprintf('%03d', (int)$accountID % 1000);
    }

    /**
     * 订单编号生成规则，n(n>=1)个订单表对应一个支付表，
     * 生成订单编号(年取1位 + $pay_id取13位 + 第N个子订单取2位)
     * 1000个会员同一微秒提订单，重复机率为1/100
     * @param $pay_id 支付表自增ID
     * @return string
     */
    public static function makeOrderSn($pay_id) {
        //记录生成子订单的个数，如果生成多个子订单，该值会累加
        static $num;
        if (empty($num)) {
            $num = 1;
        } else {
            $num++;
        }

        return 'XL' . date('YmdHis', time()) . sprintf('%010d', $pay_id) . sprintf('%02d', $num);
//        return 'EXAM' . (date('y', time()) % 9 + 1) . sprintf('%013d', $pay_id) . sprintf('%02d', $num);
    }
}

